package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.LecTeacherAndCourseDTO;
import com.cheer.entity.LecTeacherEntity;
import com.cheer.entity.OaLearnClassEntity;
import com.cheer.param.LecTeacherListParam;
import com.cheer.vo.LecTeacherCourseDetailVo;
import com.cheer.vo.LecTeacherVO;
import com.cheer.vo.TeacherLectureCountVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@Mapper
@Repository
public interface LecTeacherDao extends BaseMapper<LecTeacherEntity> {

    default IPage<LecTeacherEntity> selectPageByQueryCondition(Integer pageNum, Integer pageSize, String phone, String path) {
        QueryWrapper<LecTeacherEntity> queryWrapper = new QueryWrapper<>();

        String trimPhone = StringUtils.trimToNull(phone);
        if (trimPhone != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .eq("phone", trimPhone));
        }

        if (path != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .likeRight("path", path));
        }

        return selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    default List<LecTeacherEntity> selectListByPath(String path, String phone) {
        QueryWrapper<LecTeacherEntity> queryWrapper = new QueryWrapper<>();

        String trimPhone = StringUtils.trimToNull(phone);
        if (trimPhone != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .eq("phone", trimPhone));
        }

        if (path != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .likeRight("path", path));
        }

        return selectList(queryWrapper);
    }

    default String selectName(Integer teacherId) {
        LecTeacherEntity entity = selectById(teacherId);
        if (entity != null) {
            return entity.getName();
        }
        return null;
    }

    default LecTeacherEntity selectByAccount(String adTel) {
        return selectOne(new QueryWrapper<LecTeacherEntity>().eq("account",  adTel).last("limit 1"));
    }

    IPage<LecTeacherVO> listByPage(Page<LecTeacherVO> page, @Param("lecTeacherListParam") LecTeacherListParam lecTeacherListParam);


    List<LecTeacherVO> list( @Param("lecTeacherListParam") LecTeacherListParam lecTeacherListParam );

    List<LecTeacherVO> list1(@Param("lecTeacherListParam") LecTeacherListParam lecTeacherListParam);


    default int countByPath(String path) {

        QueryWrapper<LecTeacherEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
               .likeRight("path", path));

        return selectCount(queryWrapper);
    }

    @Update("update lec_teacher set score = score + #{score} where id = #{id}")
    void updateScoreByTeacherId(@Param("id") Integer id, @Param("score") int score);

//    @Select("select lc.teacher_id, lt.`name`, lc.num,lt.score from (select teacher_id, count(teacher_id) as num from lec_course\n" +
//            "\twhere start_time >= #{startTime} AND end_time <= #{endTime} group by teacher_id) as lc INNER JOIN lec_teacher lt on lt.id = lc.teacher_id order by lc.num desc ")
    List<TeacherLectureCountVo> queryTeacherLectureCount(@Param("startTime") Date startTime,
                                                         @Param("endTime") Date endTime,
                                                         @Param("type") Integer type);

    @Select("SELECT\n" +
            "\tlt.`name`,\n" +
            "\tlt.score,\n" +
            "\tlc.teacher_id,\n" +
            "\tIFNULL(course_type,0) as course_type,\n" +
            "\tcount( 0 ) num \n" +
            "FROM\n" +
            "\tlec_course lc\n" +
            "\tINNER JOIN lec_teacher lt ON lt.id = lc.teacher_id \n" +
            "WHERE\n" +
            "\tlc.teacher_id IN (\n" +
            "\tSELECT\n" +
            "\t\tlc.teacher_id \n" +
            "\tFROM\n" +
            "\t\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tteacher_id,\n" +
            "\t\t\tcount( teacher_id ) AS num \n" +
            "\t\tFROM\n" +
            "\t\t\tlec_course \n" +
            "\t\tWHERE\n" +
            "\t\t\tstart_time >= #{startTime} AND end_time <= #{endTime} \n" +
            "\t\tGROUP BY\n" +
            "\t\t\tteacher_id \n" +
            "\t\t) AS lc\n" +
            "\t\tINNER JOIN (select id,name,score,types from lec_teacher where types LIKE CONCAT( '%', #{type}, '%' )) lt ON lt.id = lc.teacher_id \n" +
            "\tORDER BY\n" +
            "\t\tlc.num DESC \n" +
            "\t) \n" +
            "GROUP BY\n" +
            "\tlc.teacher_id,\n" +
            "\tcourse_type \n" +
            "HAVING\n" +
            "\tteacher_id = #{teacherId}")
    List<LecTeacherCourseDetailVo> queryCourseTypeSeries(@Param("startTime") Date startTime,
                                                         @Param("endTime") Date endTime,
                                                         @Param("teacherId") Long teacherId,
                                                         @Param("type") Integer type);
}
